import React, { Component } from 'react';
import axios from './../../axios';
import { Tree } from 'antd';
import './index.less';

const { DirectoryTree } = Tree;

class TreeFolder extends Component {
	
	state = {
		treeMenu: []
	}
	
	componentDidMount(){
		this.getDir('0');
	}
	
	//遍历文件夹目录树
	renderTree = (data) => {
		return data.map((item, index)=>{
			let key = item.fileid;
			let title = item.filename;
			if(item.children){
				return (
					{title:title, key:key.toString(), children: this.renderTree(item.children)}
				)
			}
			return {title:title, key:key.toString()}
		})
	}
	
	//获取文件夹目录树
	getDir = (fileid) => {
		axios.get({
			url: "/files/page/local", 
			method: "get",
			params: {pid: 0},
		}).then(res=>{
			if(res.code === 0){
				if(res.result.list.length > 0){
					let resData = this.renderTree(res.result.list);
					this.setState({
						treeMenu: resData
					})
				}
			}
		})
	}
	
	onSelect = (keys, event) => {
		this.props.dirChecked(keys[0]);
	};

	render(){
		return <div className="tree-folder-wrap">
			<DirectoryTree
				defaultExpandedKeys={["0"]}
				onSelect={this.onSelect}
				treeData={this.state.treeMenu}
			/>
		</div>
	}
}

export default TreeFolder;